﻿module Menu
{
	export class LoginModal
	{

		public onLogin() { }
		public onCancel() { }

		private modal: JQuery;
		private usernameTb: JQuery;
		private passwordTb: JQuery;
		public getUsername(): string
		{
			return this.usernameTb.val();
		}

		public getPassword(): string
		{
			return this.passwordTb.val();
		}

		public init()
		{
			this.usernameTb = $('#usernameTb');
			this.passwordTb = $('#passwordTb');
			var thethis = this;
			this.modal = $('#loginModal');
			$('#loginbutton').on('click', function ()
			{
				thethis.onLogin();

			});

			$('#cancelbutton').on('click', function ()
			{
				thethis.onCancel();
			});
		}

		public show(): void
		{
			this.modal["foundation"]('reveal', 'open');
		}

		public hide(): void
		{
			this.modal["foundation"]('reveal', 'close');
		}


	}

	export class LoginFailedModal
	{

		public onAgain() { }
		public onCancel() { }
		private modal: JQuery;

		public init()
		{

			var thethis = this;
			this.modal = $('#loginFailedModal');

			$('#_nhaplaiBt').on('click', function ()
			{
				thethis.onAgain();

			});

			$('#_cancelbutton').on('click', function ()
			{
				thethis.onCancel();
			});
		}

		public show(): void
		{
			this.modal["foundation"]('reveal', 'open');
		}

		public hide(): void
		{
			this.modal["foundation"]('reveal', 'close');
		}
	}

	export class Login
	{

		private loginModal = new LoginModal();
		private waitModal = new Modal.Wait();
		private loginFaliedModal = new LoginFailedModal();
		private errorModal = new Modal.Error();

		private usernameTb: JQuery;
		private passwordTb: JQuery;


		public onLoginSuccessed(param?: any): void
		{
			location.reload();
		}

		public onLoginCanceled(param?: any): void
		{
		}

		public init(): void
		{
			this.errorModal.init();
			this.loginModal.init();
			this.waitModal.init();
			this.loginFaliedModal.init();


			var thethis = this;
			WindJS.wire(this.loginFaliedModal, "onAgain", function ()
			{

				thethis.loginModal.show();
			});

			WindJS.wire(this.loginFaliedModal, "onCancel", function ()
			{
				thethis.loginFaliedModal.hide();
				thethis.onLoginCanceled();
			});

			WindJS.wire(this.loginModal, "onCancel", function ()
			{
				thethis.loginModal.hide();
				thethis.onLoginCanceled();
			});

			WindJS.wire(this.loginModal, "onLogin", function ()
			{
				thethis.login();
			});

		}

		public login(): void
		{

			var thethis = this;
			thethis.waitModal.show();

			$.post("/User/Login", {
				username: thethis.loginModal.getUsername(),
				password: thethis.loginModal.getPassword()
			}, function (json: any)
			{
					console.debug(json);
					var data = json == "" ? {} : JSON.parse(json);
					if (data.hasOwnProperty("username") == false)
					{
						thethis.loginFaliedModal.show();
						thethis.onLoginCanceled({});
					}
					else
					{
						setTimeout(function ()
						{
							thethis.waitModal.show();
						}, 1000);
						thethis.onLoginSuccessed(data);
					}


				}).fail(function (xhr, textStatus, errorThrown)
				{
					thethis.onLoginCanceled();
					thethis.errorModal.changeMessage('Kết nối mạng có vấn đề, xin thử lại sau ít phút nữa');
					thethis.errorModal.show();
				});
		}
	}
}

var s0169_login = new Menu.Login();
s0169_login.init();